<?php
  function dbConnect(){
    global $host;
    global $user;
    global $pass;
    global $lang;
    global $db;
    
    global $link;
    $link = mysql_connect($host, $user, $pass) or die($lang["FAILED_TO_CONNECT"]);
    mysql_select_db($db, $link) or die($lang["FAILED_TO_SELECT_DB"].': '.mysql_error($link));
}
function hasAccess($flag){
    if(isset($_SESSION[$flag]) && $_SESSION[$flag])
        return true;
    return false;
}

function addCheckbox($lngName, $name, $item, $default, $surfix){
    global $lang;
    $checked=' ';
    if(($item!=null&&isset($item[$name])))
    {   if($item[$name] == 1)
            $checked = ' checked="true"';
    } else if($default){
         $checked = ' checked="true"';
    }
    echo '
        <th class="right">'.$lang[$lngName].'</th>  
        <td>
            <input class="center" type="checkbox" name="'.$name.$surfix.'" id="'.$name.$surfix.'"'.$checked;
     if($item!=null){
        echo ' disabled="true" />
            <input class="hidden" type="checkbox" name="h'.$name.$surfix.'" id="h'.$name.$surfix.'"'.$checked.' disabled="true" />';
     } else {
        echo ' />';   	
    }
}

function addText($lngName, $name, $item, $default, $surfix, $numOnly, $class=null){  
    global $lang;  
    
    echo '
        <th class="right">'.$lang[$lngName].'</th>  
        <td>
            <input type="text" name="'.$name.$surfix.'" id="'.$name.$surfix.'" value="'.(($item!=null&&isset($item[$name]))?$item[$name]:$default ).'" ';
    if($class!=null){
        echo ' class="'.$class.'" ';
    }
    if($numOnly){
        echo ' onkeypress="return numOnlyInput(event)"';
    }        
     if($item!=null){
        echo ' disabled="true" />
            <input class="hidden" type="text" name="h'.$name.$surfix.'" id="h'.$name.$surfix.'" value="'.(($item!=null&&isset($item[$name]))?$item[$name]:$default).'" disabled="true" />';
     } else {
        echo ' />';   	
    }
}

function createInsertStatement($fields, $table_name, $isPost){
    $query = 'INSERT INTO '.$table_name;
    $flds = '';
    $values = '';
    $val = '';
    foreach($fields as $fld=>$def){
        $val = $def;
        if($isPost && isset($_POST[$fld])){
            $val = $_POST[$fld];
        }
        else if(!$isPost && isset($_GET[$fld])){
            $val = $_GET[$fld];
        }
        if(is_string($def)){
            $val = '\''.mysql_escape_string($val).'\'';
        } else {
            if(is_string($val) && $val=='on'){
                $val = '1';
            }
        }
        if($flds != ''){
            $flds= $flds.',';
            $values= $values.',';
        }
        $flds = $flds.$fld;
        $values = $values.$val;
    }
    
    $query = 'INSERT INTO '.$table_name.' ('.$flds.') VALUES ('.$values.')';
    //echo $query.'<br />';
    return $query;
}

function createUpdateStatement($fields, $table_name, $isPost, $pk, $pk_value){
    $query = '';
    $flds = '';
    $val = '';
    
    foreach($fields as $fld=>$def) {
        $val = $def;
        if($isPost && isset($_POST[$fld])){
            $val = $_POST[$fld];
        }
        else if(!$isPost && isset($_GET[$fld])){
            $val = $_GET[$fld];
        }
        if(is_string($def)){
            $val = '\''.mysql_escape_string($val).'\'';
        } else {
            if(is_string($val) && $val=='on'){
                $val = '1';
            }
        }
        if($flds != ''){
            $flds= $flds.',';
        }
        $flds = $flds.$fld.' = '.$val;
    }
    $query = 'UPDATE '.$table_name.' SET '.$flds.' WHERE '.$pk.' = \''.mysql_escape_string($pk_value).'\'';
    //echo $query.'<br />';
    
    return $query;
}
 
?>
